<template>
  <van-nav-bar
    title="修改地址"
    left-text="返回"
    left-arrow
    @click-left="goAddress"
  />

  <van-address-edit
    :area-list="areaList"
    show-set-default
    :address-info="AddressInfo"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
  />
</template>

<script>
import { areaList } from "@vant/area-data";
import { showToast } from "vant";
import { service } from "../api/config";

//取出缓存存储的地址信息
let Info = localStorage.getItem("Info");
export default {
  name: "addressEditChirld",
  data() {
    return {
      //加载地区
      areaList,
      AddressInfo: {
        //获取当前编辑的地址信息
        name: JSON.parse(Info)["name"],
        tel: JSON.parse(Info)["tel"],
        province: JSON.parse(Info)["province"],
        city: JSON.parse(Info)["city"],
        county: JSON.parse(Info)["county"],
        addressDetail: JSON.parse(Info)["addressDetail"],
        isDefault: JSON.parse(Info)["isDefault"],
        id: JSON.parse(Info)["id"],
        areaCode: "",
      },
    };
  },
  methods: {
    goAddress() {
      this.$router.push("/address");
    },
    onSave(value) {
      console.log(value);
      service
        .post("/wx/address/save", {
          name: value.name,
          tel: value.tel, //字符串截取。电话格式
          province: value.province,
          city: value.city,
          county: value.county,
          addressDetail: value.addressDetail,
          isDefault: value.isDefault == true ? "true" : "false",
          id: value.id,
        })
        .then((res) => {
          console.log(res);
          if (res.data.errmsg == "成功") {
            this.isSaving == true;
            showToast("地址修改成功,2秒后跳转到地址管理");
            setTimeout(() => {
              this.$router.push("address");
            }, 2000);
            //跳转后清除地址缓存
            localStorage.removeItem("Info");
          } else {
            showToast("地址修改失败");
          }
        });
    },
  },
};
</script>